package org.ricks.net.action;

import org.ricks.ioc.utils.ReflectUtils;
import org.ricks.net.action.context.FlowContext;
import org.ricks.net.action.context.head.HeadMeta;

import java.lang.reflect.Method;

public class ActionHandler implements ActionReceiver {

    // 观察者的bean
    protected final Object bean;

    // 被ActionMethod注解标注的方法
    protected final Method method;

    // 事件接收方式
    protected final Task task;

    // 接收的参数Class
    protected final Class<? extends FlowContext<? extends HeadMeta>> contextClazz;

    public ActionHandler(Object bean, Method method, Task task, Class<? extends FlowContext<? extends HeadMeta>> contextClazz) {
        this.bean = bean;
        this.method = method;
        this.task = task;
        this.contextClazz = contextClazz;
        this.method.setAccessible(true);
    }

    @Override
    public Task task() {
        return task;
    }

    @Override
    public void invoke(FlowContext<? extends HeadMeta> event) {
        ReflectUtils.invokeMethod(bean, method, event);
    }

    @Override
    public Object getBean() {
        return bean;
    }


    public Method getMethod() {
        return method;
    }

    public Task getTask() {
        return task;
    }

    public Class<? extends FlowContext> getContextClazz() {
        return contextClazz;
    }
}
